package com.example.smartcitydemo.viewModel.job

import androidx.lifecycle.*
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.bean.*


import com.example.smartcitydemo.ext.getLiveData
import com.example.smartcitydemo.repo.work.WorkRepo


/**
 * @PACKAGE_NAME: com.example.smartcitydemo.viewmodel.job
 * @NAME: Caleb.F
 * @DATE: 2021/11/16 20:22
 * @Email: 1919796283@qq.com
 */
class WorkViewModel : ViewModel() {
    private val mutableJobLiveData: MutableLiveData<String> = MutableLiveData()

    private val mutableJobListLiveData: MutableLiveData<Int> = MutableLiveData()

    private val mutableJobSearchLiveData: MutableLiveData<String> = MutableLiveData()

    private val mutableDeliverId: MutableLiveData<Int> = MutableLiveData()

    val jobTypeLiveData: LiveData<Resource<List<JobTypeBean>>> = mutableJobLiveData.switchMap {
        getLiveData {
            WorkRepo.getJobType()
        }
    }

    val jobListData: LiveData<Resource<List<JobListBean>>> = mutableJobListLiveData.switchMap {
        getLiveData {
            when (it) {
                //获取全部职位列表
                0 -> WorkRepo.getJobListAll()
                //获取某个职位下的详细信息
                else -> WorkRepo.getJobListById(it)
            }
        }

    }
    val deliverHistoryLiveData: LiveData<Resource<List<DeliverHistoryBean>>> =
        mutableDeliverId.switchMap {
            getLiveData {
                WorkRepo.getDeliverList(it)
            }
        }


    val jobSearchValue: LiveData<Resource<List<JobListBean>>> = mutableJobSearchLiveData.switchMap {
        getLiveData {
            WorkRepo.getJobTypeByProfessionName(it!!)
        }
    }

    fun getJobType() {
        mutableJobLiveData.value = mutableJobLiveData.value
    }

    fun getJobList(professionalId: Int) {
        mutableJobListLiveData.value = professionalId
    }

    fun getJobSearchValue(searchValue: String) {
        mutableJobSearchLiveData.value = searchValue
    }

    fun getDiliverHistory(userId: Int) {
        mutableDeliverId.value = userId
    }


    private val mutableCompanyId = MutableLiveData<Int>()
    private val mutablePostId = MutableLiveData<Int>()
    private val mutableJobPostData = MutableLiveData<DeliverPostBean>()

    val companyDetail = mutableCompanyId.switchMap {
        getLiveData { WorkRepo.getCompanyDetail(it) }
    }

    val postDetail = mutablePostId.switchMap {
        getLiveData { WorkRepo.getJobDetail(it) }
    }

    val jobPostResult = mutableJobPostData.switchMap {
        getLiveData { WorkRepo.postDeliver(it) }
    }

    fun getCompanyDetail(id: Int) {
        mutableCompanyId.value = id
    }

    fun getPostDetail(id: Int) {
        mutablePostId.value = id
    }

    fun postDeliver(deliver: DeliverPostBean) {
        mutableJobPostData.value = deliver
    }

    private val mutableResumeData: MutableLiveData<String> = MutableLiveData()
    val personResumeData = mutableResumeData.switchMap {
        getLiveData { WorkRepo.getPersonResumeInfo() }
    }

    fun getResumeList() {
        mutableResumeData.value = mutableResumeData.value
    }


    private val mutableAddPersonResume: MutableLiveData<ResumePostBean> = MutableLiveData()
    fun addPersonResume(resumePostBean: ResumePostBean) {
        mutableAddPersonResume.value = resumePostBean
    }

    val addResumeResult = mutableAddPersonResume.switchMap {
        getLiveData {
            WorkRepo.addPersonResume(it)
        }
    }

    private val mutableModifyPersonData: MutableLiveData<ResumeModifyBean> = MutableLiveData()
    fun modifyPersonData(resumeModifyBean: ResumeModifyBean) {
        mutableModifyPersonData.value = resumeModifyBean
    }
    val modifyResult = mutableModifyPersonData.switchMap {
        getLiveData {
            WorkRepo.modifyPersonResume(it )
        }
    }

}